/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trunk.control;
import java.util.Scanner;
/**
 *
 * @author Jennifer Gan
 */
public class CPU8086 {
    public void mySleep(){
        try {
             Thread.sleep(1000);                
        } catch (InterruptedException e) {
             return;
      }  
    
    }
    public void printFT(){
        System.out.println("FT:  M->IR,PC+1->PC");
        mySleep();
    }
    public void printST(int i){
        switch(i)
        {
            case 2:
                System.out.println("ST0: R0->MAR");
                mySleep();
                System.out.println("ST1: M->MDR->C");
                mySleep();
                break;
            case 3:
                System.out.println("ST0: R0-1->R0，MAR");
                mySleep();
                System.out.println("ST1: M->MDR->C");
                mySleep();
                break;
            case 4:
                System.out.println("ST0: R0->MAR");
                mySleep();
                System.out.println("ST1: M->MDR->C");
                mySleep();
                System.out.println("ST2: R0+1->R0");
                mySleep();
                break;
            case 5:
                System.out.println("ST0: R0->MAR");
                mySleep();
                System.out.println("ST1: M->MDR->C");
                mySleep();
                System.out.println("ST2: R0+1->R0");
                mySleep();
                System.out.println("ST3: C->MAR");
                mySleep();
                System.out.println("ST4: M->MDR->C");
                mySleep();
                break;
            case 6:
                System.out.println("ST0: PC->MAR");
                mySleep();
                System.out.println("ST1: M->MDR->C");
                mySleep();
                System.out.println("ST2: PC+1->PC");
                mySleep();
                System.out.println("ST3: C+R0->MAR");
                mySleep();
                System.out.println("ST4: M->MDR->C");
                mySleep();
                break;
        }
    }
    public void printDT(String s,int i){
        if(s.equals("mov"))
        {
                switch(i)
                {
                    case 2:
                        System.out.println("DT0: R1->MAR");
                        mySleep();
                        break;
                    case 3:
                        System.out.println("DT0: R1-1->R0，MAR");
                        mySleep();
                        break;
                    case 4:
                        System.out.println("DT0: R1->MAR");
                        mySleep();
                        System.out.println("DT1: R1+1->R0");
                        mySleep();
                        break;
                    case 5:
                        System.out.println("DT0: R1->MAR");
                        mySleep();
                        System.out.println("DT1: R1+1->R1");
                        mySleep();
                        System.out.println("DT2: M->MDR->MAR");
                        mySleep();
                        break;
                    case 6:
                        System.out.println("DT0: PC->MAR");
                        mySleep();
                        System.out.println("DT1: M->MDR->D");
                        mySleep();
                        System.out.println("DT2：PC+1->PC");
                        mySleep();
                        System.out.println("DT3: D+R1->MAR");
                        mySleep();
                        break;
        }
        }else if(s.equals("add")||s.equals("inc")){
            switch(i)
                {
                    case 2:
                        System.out.println("DT0: R1->MAR");
                        mySleep();
                        System.out.println("DT1: M->MDR->D");
                        mySleep();
                        break;
                    case 3:
                        System.out.println("DT0: R1-1->R0，MAR");
                        mySleep();
                        System.out.println("DT1: M->MDR->D");
                        mySleep();
                        break;
                    case 4:
                        System.out.println("DT0: R1->MAR");
                        mySleep();
                        System.out.println("DT1: M->MDR->D");
                        mySleep();
                        System.out.println("DT2: R1+1->R0");
                        mySleep();
                        break;
                    case 5:
                        System.out.println("DT0: R1->MAR");
                        mySleep();
                        System.out.println("DT1: M->MDR->D");
                        mySleep();
                        System.out.println("DT2: R1+1->R1");
                        mySleep();
                        System.out.println("DT3: D->MAR");
                        mySleep();
                        System.out.println("DT4: M->MDR->D");
                        mySleep();
                        break;
                    case 6:
                        System.out.println("DT0: PC->MAR");
                        mySleep();
                        System.out.println("DT1: M->MDR->D");
                        mySleep();
                        System.out.println("DT2：PC+1->PC");
                        mySleep();
                        System.out.println("DT3: D+R1->MAR");
                        mySleep();
                        System.out.println("DT4: M->MDR->D");
                        mySleep();
                        break;
            }
        }
    }
    public void printET(String s,int m,int n){
         if(s.equals("mov"))
        {
                if(m==1&&n==1){
                        System.out.println("ET0: R0->R1");
                        mySleep();
                        System.out.println("ET1: PC->MAR");
                        mySleep();
                }else if(m==1&&(n==2||n==3||n==4||n==5||n==6)){
                        System.out.println("ET0: R0->MDR");
                        mySleep();
                        System.out.println("ET1: MDR->M");
                        mySleep();
                        System.out.println("ET2: PC->MAR");
                        mySleep();
                }else if(n==1&&(m==2||m==3||m==4||m==5||m==6)){
                        System.out.println("ET0: C->R1");
                        mySleep();
                        System.out.println("ET1: PC->MAR");
                        mySleep();
                }else{
                        System.out.println("ET0: C->MDR");
                        mySleep();
                        System.out.println("ET1: MDR->M");
                        mySleep();
                        System.out.println("ET2: PC->MAR");
                        mySleep();
                }
              
        }else if(s.equals("add")){
            if(m==1&&n==1){
                        System.out.println("ET0: R0+R1->R1");
                        mySleep();
                        System.out.println("ET1: PC->MAR");
                        mySleep();
                }else if(m==1&&(n==2||n==3||n==4||n==5||n==6)){
                        System.out.println("ET0: R0+D->MDR");
                        mySleep();
                        System.out.println("ET1: MDR->M");
                        mySleep();
                        System.out.println("ET2: PC->MAR");
                        mySleep();
                }else if(n==1&&(m==2||m==3||m==4||m==5||m==6)){
                        System.out.println("ET0: C+R1->R1");
                        mySleep();
                        System.out.println("ET1: PC->MAR");
                        mySleep();
                }else{
                        System.out.println("ET0: C+D->MDR");
                        mySleep();
                        System.out.println("ET1: MDR->M");
                        mySleep();
                        System.out.println("ET2: PC->MAR");
                        mySleep();
                }    
        }else{
            if(n==1){
                        System.out.println("ET0: R1+1->R1");
                        mySleep();
                        System.out.println("ET1: PC->MAR");
                        mySleep();
            }else{
                        System.out.println("ET0: D+1->MDR");
                        mySleep();
                        System.out.println("ET1: MDR->M");
                        mySleep();
                        System.out.println("ET2: PC->MAR");
                        mySleep();
            }
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        CPU8086 cpu;
        cpu=new CPU8086();
        System.out.println("请输入指令：");
        Scanner s=new Scanner(System.in);
        String instruction=s.nextLine();
        System.out.println("请选择源操作数寻址方式: 1:R 2:(R) 3:-(R) 4:(R)+ 5:@(R)+ 6:X(R) ");
        int mode_s=s.nextInt();
        System.out.println("请选择目的操作数寻址方式: 1:R 2:(R) 3:-(R) 4:(R)+ 5:@(R)+ 6:X(R) ");
        int mode_d=s.nextInt();
        if(instruction.equals("mov")||instruction.equals("add")){
            cpu.printFT();
            cpu.printST(mode_s);
            cpu.printDT(instruction, mode_d);
            cpu.printET(instruction, mode_s, mode_d);
        }else{
            cpu.printFT();
            cpu.printDT(instruction, mode_d);
            cpu.printET(instruction, mode_s, mode_d);
        }
    }
}
